package com.wander.nio.chat.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class NioChatClient {
    public static void main(String[] args) {
        try {
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress("localhost", 8888));
            System.out.println("请输入用户名：");
            Scanner scanner = new Scanner(System.in);
            String username = scanner.nextLine();
            //循环从控制台输入消息
            while (true) {
                String msg = username + ": " + scanner.nextLine();
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                socketChannel.write(buffer.wrap(msg.getBytes("UTF-8")));
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
